<template>
  <el-container>
    <el-header>
      <el-page-header @back="goBack" content="我的空间 "></el-page-header>
    </el-header>

    <a-col :span="6">
      <div class="user-info">
        <h2>个人信息</h2>
        <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="用户名" prop="username" class="form-item">
            <el-input v-model="formData.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" class="form-item">
            <el-input type="password" v-model="formData.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm" class="user-info-alter">修改</el-button>
          </el-form-item>
        </el-form>
      </div>
    </a-col>

    <div class="post-list">
      <el-card v-for="post in posts" :key="post.id" @click.native="goToshowPost(post.postId)" class="post-card">
        <div class="post-title">{{ post.title }}</div>
        <div class="post-info">
          <span class="post-date">{{ post.postTime }}</span>
        </div>
        <div class="post-content">{{ post.content }}</div>
        <el-button @click="goToUpdate(post.postId)" type="primary">修改</el-button>
      </el-card>
    </div>


  </el-container>
</template>

<script>
export default {
  data() {
    return {
      posts: [],
      formData: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 8, message: "密码长度不能少于8位", trigger: "blur" },
          { max: 20, message: "密码长度不能超过20位", trigger: "blur" },
        ],
      },
    }
  },
  async created() {
    this.userId = this.$route.params.userId;
    this.userName = this.$route.params.userName;
    this.backPage = this.$route.params.backPage;
    await this.$axios.get('https://localhost:44300/api/User/GetInfor', {
      params: {
        id: this.userId,
      }
    })
      .then(response => {
        this.formData.username = response.data.response.userName;
        this.formData.password = response.data.response.password;
      })
      .catch(error => {
        console.error(error);
      });

    await this.$axios.get('https://localhost:44300/api/Post/PosterSelect', {
      params: {
        poster: this.userName
      }
    })
      .then(response => {
        this.posts = response.data.response;
      })
      .catch(error => {
        console.error(error);
      });

  },
  methods: {
    goToPostPage(post) {
      this.$router.push({ path: '/post/edit', query: { id: post.id, title: post.title, category: post.category, content: post.content } });
    },
    goToPost(id) {
      // 跳转到帖子详情页面
      this.$router.push(`/post/${id}`);
    },
    goBack() {
      this.$router.push({ name: this.backPage === 'adminMain' ? 'adminMain' : 'afterLogin', params: { userId: this.userId, userName: this.userName } })
    }, submitForm() {
      this.$axios
        .get('https://localhost:44300/api/User/UpdateUser', {
          params: {
            userId: this.userId,
            Username: this.formData.username,
            password: this.formData.password,
          },
        })
        .then((response) => {
          if (response.data.success === true) {
            this.$message({
              message: '修改成功',
              type: 'success',
            });
          } else {
            this.$message({
              message: response.data.msg,
              type: 'error',
            });
          }
        })
        .catch((error) => {
          this.$message.error(error.message);
        });
    },
    goToUpdate(postId) {
      this.$router.push({ name: 'updatePost', params: { postId: postId, userId: this.userId, userName: this.userName } });
    }, goToshowPost(postId) {
      this.$router.push({ name: 'showPost', params: { postId: postId, userId: this.userId, userName: this.userName } });
    }
  },
};
</script>

<style scoped>
.user-info {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f7f7f7;
  border-radius: 5px;
  box-shadow: 0 0 10px #aaa;
  margin-bottom: 20px;
}

h2 {
  margin-top: 0;
}

.form-item {
  margin-bottom: 20px;
}

.post-list {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.post-card {
  width: 80%;
  margin-bottom: 16px;
  cursor: pointer;
}

.post-title {
  font-size: 24px;
  margin-bottom: 8px;
}

.post-info {
  font-size: 14px;
  color: #888;
  margin-bottom: 8px;
}

.post-category {
  margin-right: 8px;
}

.post-summary {
  font-size: 16px;
  line-height: 1.5;
}
</style>
